﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using BusinessLogic;
namespace WebTeamplate
{
    public partial class Admin_CategoryDetail : BasePage
    {
        #region Fields
        public string urlList = "ManageCategory.aspx";
        #endregion

        #region ViewStates
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }
        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            if (!Page.IsPostBack)
            {
                InitTextControl();
                InitParamater();
                LoadData();
                txtCategoryId.Focus();
            }

        }
        private void InitParamater()
        {
            if (Request.QueryString["id"] != null)
                txtCategoryId.Text = Request.QueryString["id"].ToString();
            // this.Command = (Request.QueryString["command"] == null ? CommonFunction.DEFAULT_COMMAND_ADD : Request.QueryString["id"].ToString());

        }
        #endregion

        #region Methos
        private void LoadData()
        {
            if (txtCategoryId.Text != string.Empty)
            {
                Category objCate = CategoryUtil.Get(txtCategoryId.Text);
                if (objCate != null)
                {
                    txtCategoryName.Text = objCate.CategoryName;
                    if(objCate.IsPublished)
                            drlPublish.SelectedValue = "1";
                    else drlPublish.SelectedValue = "0";
                    txtCategoryId.Enabled = false;
                    this.Command = CommonFunction.DEFAULT_COMMAND_EDIT;
                }
                else this.Command = CommonFunction.DEFAULT_COMMAND_ADD;
            }
            else this.Command = CommonFunction.DEFAULT_COMMAND_ADD;
        }
        private void InitTextControl()
        {
            string validateGroup = "cate";
            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCacel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            refvCategoryId.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblCategoryId));
            refvCategoryId.ValidationGroup = validateGroup;
            refvCategoryName.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblName));
            refvCategoryName.ValidationGroup = validateGroup;
            refvPublish.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblPublish));
            refvPublish.ValidationGroup = validateGroup;
            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;
            CommonFunction.InitStatusPublishForComboboxDetail(drlPublish);

        }
        private void UpdateCate(Category objCategory)
        {
            if (!CategoryUtil.Update(objCategory))
                lblErrorMessage.Text = string.Format(Resources.Resource.MESSAGE_INSERT_ERROR, CommonFunction.GetTextLabel(lblCategoryId));
            else
                Response.Redirect(urlList);

        }
        private void InsertCate(Category objCategory)
        {
            int result = CategoryUtil.Insert(objCategory);
            if (result == -1)//exists
                lblErrorMessage.Text = string.Format(Resources.Resource.MESSAGE_EXISTS_ITEM, CommonFunction.GetTextLabel(lblCategoryId));
            else if (result == 0)//error
                lblErrorMessage.Text = string.Format(Resources.Resource.MESSAGE_INSERT_ERROR, CommonFunction.GetTextLabel(lblCategoryId));
            else
                Response.Redirect(urlList);

        }

        #endregion

        #region Events, Handler
        protected void btnCacel_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlList);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Category objCategory = new Category();
                objCategory.CategoryId = txtCategoryId.Text;
                objCategory.CategoryName = txtCategoryName.Text;
                if(drlPublish.SelectedValue=="1")
                objCategory.IsPublished = true;
                else objCategory.IsPublished = false;
                objCategory.CreatedBy = this.userLoginName;
                objCategory.CreatedDate = CommonFunction.Now;
                objCategory.LastUpdatedBy = this.userLoginName;
                objCategory.LastUpdatedDate = objCategory.CreatedDate;
                if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
                    InsertCate(objCategory);
                else
                    UpdateCate(objCategory);

            }
        }


        #endregion       
 
    }
}